package com.hfi.ood.mailsendproduct.good;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Mail相当于是对User的一个Wrapper
 * @author ChangLiang
 * @date 2020/5/12
 */
public class Mail {

    /**
     * Mail是与User关联的
     */
    private User user;

    private MailBodyTemplate bodyTemplate;

    public Mail(User u, MailBodyTemplate bodyTemplate){
        this.user = u;
        this.bodyTemplate = bodyTemplate;
    }
    public String getAddress(){
        return user.getEMailAddress();
    }
    public String getSubject(){
        return "您关注的产品降价了";
    }
    public String getBody(){

//        return "尊敬的 "+user.getName()+", 您关注的产品 " + this.buildProductDescList() + " 降价了，欢迎购买!" ;

        Map<String, String> map = new HashMap<>(2);
        map.put("name", user.getName());
        map.put("productString", this.buildProductDescList());
        String render = bodyTemplate.render(map);
        return render;

    }
    private String buildProductDescList() {
        List<Product> products = user.getSubscribedProducts();
        StringBuilder builder = new StringBuilder();
        products.stream().forEach(product->builder.append(product.getDescription()).append(","));
        return builder.length() > 0 ? builder.substring(0, builder.length() - 1) : builder.toString();
    }
}
